package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.mgpos.activities.R;
import com.moleq.models.NetworkSetting;

public class NetworkAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<NetworkSetting> datasource;
	private int selectedRow = -1;
	private Bitmap activeTrue;
	private Bitmap activeFalse;

	public NetworkAdapter(Context context, List<NetworkSetting> listItem) {
		this.mInflater = LayoutInflater.from(context);
		this.datasource = listItem;
		activeTrue = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.presence_online);
		activeFalse = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.presence_invisible);
	}

	public void setSource(List<NetworkSetting> listItem) {
		this.datasource = listItem;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return datasource.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datasource.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.networking_row, null);
			holder.net_no = (TextView) convertView.findViewById(R.id.tv_net_id);
			holder.net_name = (TextView) convertView.findViewById(R.id.tv_net_name);
			holder.img_check = (ImageView) convertView.findViewById(R.id.img_net_check);
			holder.ll_check = (LinearLayout) convertView.findViewById(R.id.lv_net_check);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String desc = datasource.get(position).getStation();
		if (datasource.get(position).isLocal()) {
			desc += '*';
		}
		holder.net_no.setText(desc);
		holder.net_name.setText(String.valueOf(datasource.get(position).getNs_ip()));

		if (datasource.get(position).isOnline() ||datasource.get(position).isLocal()) {
			holder.img_check.setImageBitmap(activeTrue);
		} else {
			holder.img_check.setImageBitmap(activeFalse);
		}

		if (position == selectedRow) {
			holder.net_no.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.net_name.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.ll_check.setBackgroundColor(Color.rgb(255, 219, 16));

		} else {
			holder.net_no.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.net_name.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_check.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	public final class ViewHolder {
		public TextView net_no;
		public TextView net_name;
		public ImageView img_check;
		public LinearLayout ll_check;
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}
	
	public NetworkSetting getSelectItem() {
		return datasource.get(selectedRow);
	}
};
